using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace wowitemsearch
{
    public partial class Form1 : Form
    {
        Dictionary<string, string> bosses = new Dictionary<string, string>();
        Dictionary<string, string> zones = new Dictionary<string, string>();

        public Form1()
        {
            InitializeComponent();
        }

        private void tSearchItem_TextChanged(object sender, EventArgs e)
        {

        }

        private void bSearch_Click(object sender, EventArgs e)
        {
            int start = 45620;
            int end = 45660;

            progressBar.Value = 0;
            progressBar.Maximum = end - start;

            for (int i = start; i < end; i++)
            {
                string xml_adress = "http://www.wowhead.com/?item=" + i.ToString() +"&xml";
                XmlDocument doc = new XmlDocument();
                doc.Load(xml_adress);

                if (doc["wowhead"]["item"] != null)
                {
                    // Alleen epic items toevoegen
                    if (int.Parse(doc["wowhead"]["item"]["quality"].GetAttribute("id")) >= 4)
                    {
                        int startIndex;
                        int endIndex;

                        string itemName = doc["wowhead"]["item"]["name"].InnerText;
                        string itemID = doc["wowhead"]["item"].GetAttribute("id");

                        // Dropinfo is in json-element
                        string dropinfo = doc["wowhead"]["item"]["json"].InnerText;
                        startIndex = dropinfo.IndexOf("more:[{");
                        if (startIndex > -1)
                        {
                            endIndex = dropinfo.IndexOf("}", startIndex);
                            dropinfo = dropinfo.Substring(startIndex + 6, endIndex - startIndex - 5);
                        }

                        startIndex = dropinfo.IndexOf("z:");
                        string zoneID = "";
                        if (startIndex > -1)
                        {
                            endIndex = dropinfo.IndexOf(",", startIndex);
                            if (endIndex <= -1)
                            {
                                endIndex = dropinfo.IndexOf("}", startIndex);
                            }
                            zoneID = dropinfo.Substring(startIndex + 2, endIndex - startIndex - 2);
                        }

                        // Alleen raid-drop-items toevoegen.
                        if (zoneID == "3456" || zoneID == "4493" || zoneID == "4500"
                             || zoneID == "4273" || zoneID == "4603")
                        {
                            startIndex = dropinfo.IndexOf("ti:");
                            string bossID;
                            string bossName;
                            if (startIndex > -1)
                            {
                                endIndex = dropinfo.IndexOf(",", startIndex);
                                bossID = dropinfo.Substring(startIndex + 3, endIndex - startIndex - 3);

                                startIndex = dropinfo.IndexOf("n:'");
                                endIndex = dropinfo.IndexOf("',", startIndex);
                                bossName = dropinfo.Substring(startIndex + 3, endIndex - startIndex - 3);
                            }
                            else
                            {
                                // Zonedrop
                                bossID = zoneID;
                                bossName = "Zone Drop";
                            }

                            startIndex = dropinfo.IndexOf("dd:");
                            string heroicBit = "0";
                            if (startIndex > -1)
                            {
                                endIndex = dropinfo.IndexOf(",", startIndex);
                                if (endIndex <= -1)
                                {
                                    endIndex = dropinfo.IndexOf("}", startIndex);
                                }
                                if (dropinfo.Substring(startIndex + 3, endIndex - startIndex - 3) == "2")
                                {
                                    heroicBit = "1";
                                }
                            }

                            if (!bosses.ContainsKey(bossID))
                            {
                                bosses.Add(bossID, bossName);
                                tQueryBoss.Text += "\r\n";
                                tQueryBoss.Text += "INSERT INTO null_sk_bosses VALUES(" + bossID + ", \"" + bossName + "\", " + zoneID + "); ";
                            }

                            if (!zones.ContainsKey(zoneID))
                            {
                                zones.Add(zoneID, "Unknown");
                                tQueryZones.Text += "\r\n";
                                tQueryZones.Text += "INSERT INTO null_sk_zones VALUES(" + zoneID + ", \"Unknown\"); ";
                            }

                            //gLoot.Rows.Add(itemName, zoneID, bossName);

                            tQueryItems.Text += "\r\n";
                            tQueryItems.Text += "INSERT INTO null_sk_items VALUES(" + itemID + ", \"" + itemName + "\", " + bossID + ", " + heroicBit + ");";
                        }
                    }
                }

                progressBar.PerformStep();
                this.Update();
            }

            MessageBox.Show("Klaar!");
        }
    }
}